package com.ypx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ypx.bean.OrderDetail;
import com.ypx.bean.Orders;
import com.ypx.bean.User;
import com.ypx.common.BaseContext;
import com.ypx.common.R;
import com.ypx.dto.OrdersDto;
import com.ypx.service.OrderDetailService;
import com.ypx.service.OrdersService;
import com.ypx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Tang
 * @date 2023/2/25
 */
@RequestMapping("/order")
@Api(tags = "订单明细")
@Slf4j
@RestController
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    /**
     * 提交订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        //1.调用service的提交订单方法
        ordersService.submit(orders);
        //2.返回结果
        return R.success("提交订单成功！");
    }

    /**
     * 订单详情
     * @param page  页码
     * @param pageSize  每页条数
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize){
        //创建分页构造器
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        //根据下单时间排序
        wrapper.orderByDesc(Orders::getOrderTime);
        //查询
        ordersService.page(ordersPage,wrapper);
        //拷贝对象
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
        List<OrdersDto> OrdersDto = ordersPage.getRecords().stream().map(item->{
            OrdersDto ordersDto = new OrdersDto();
            //拷贝对象
            BeanUtils.copyProperties(item,ordersDto);
            //查询每个订单的订单详情
            LambdaQueryWrapper<OrderDetail> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(OrderDetail::getOrderId,item.getNumber());
            List<OrderDetail> list = orderDetailService.list(wrapper1);
            ordersDto.setOrderDetails(list);
//            ordersDto.setSumNum(list.size());
            return ordersDto;
        }).collect(Collectors.toList());

        //赋值给ordersDtoPage
        ordersDtoPage.setRecords(OrdersDto);

        return R.success(ordersDtoPage);
    }

    /***
     *  后台订单管理模块，
     * @param page 页码
     * @param pageSize 每页数量
     * @param number 订单号
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,Long number,String beginTime,String endTime){
        log.info("开始时间:{},结束时间:{}",beginTime,endTime);
        //创建分页构造器
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(number!=null,Orders::getNumber,number);
        //在什么时间之间
        wrapper.between(beginTime!=null&&endTime!=null,Orders::getOrderTime,beginTime,endTime);
        ordersService.page(ordersPage,wrapper);
        BeanUtils.copyProperties(ordersPage,ordersDtoPage);
        //查询用户姓名
        List<OrdersDto> ordersDtos = ordersPage.getRecords().stream().map(item->{
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            User user = userService.getById(item.getUserId());
            ordersDto.setUserName(user.getName());
            return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(ordersDtos);
        return R.success(ordersDtoPage);
    }

    /**
     * 修改订单状态：1：待支付、2：待派送、3：已派送、4：已完成、5：已取消
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        /*UpdateWrapper<Orders> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",orders.getId());
        wrapper.set("status",orders.getStatus());

        ordersService.update(null,wrapper);*/
        ordersService.updateById(orders);


        return R.success("修改成功");
    }
}
